package com.changyou.kf.core.listener;

import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.logging.LoggingSystemProperty;
import org.springframework.context.ApplicationListener;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class StartingEventListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {

    @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {

        String envApplicationName = LoggingSystemProperty.APPLICATION_NAME.getEnvironmentVariableName();
        String envPidName = LoggingSystemProperty.PID.getEnvironmentVariableName();

        String applicationName = System.getProperty(envApplicationName);
        String pid = System.getProperty(envPidName);

        String appName = StringUtils.trimToEmpty(applicationName);
        System.setProperty("APP_NAME", appName);

        if (StringUtils.isEmpty(pid)) {
            System.setProperty(envPidName, "-");
        }

        String SERVER_IP = getServerIp();
        System.setProperty("HOSTNAME", SERVER_IP);

        System.out.println("StartingEventListener envApplicationName:" + envApplicationName);
        System.out.println("StartingEventListener applicationName:" + applicationName + "=========");
        System.out.println("StartingEventListener pid:" + pid);

        //MDC.put(envApplicationName, StringUtils.defaultIfEmpty(applicationName, "[-]"));
        //MDC.put(envPidName, StringUtils.defaultIfEmpty(pid, "-"));

        System.out.println("==================StartingEventListener==================");

    }

    /**
     * 根据网卡获得IP地址
     *
     * @return
     */
    public static String getServerIp() {
        String serverIP = "127.0.0.1";
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                String name = intf.getName();
                if (name.contains("docker") || name.contains("lo")
                        // 微软Teredo隧道网卡
                        || intf.getDisplayName().contains("Teredo")
                        // 虚拟机相关
                        || intf.getDisplayName().contains("Virtual")
                        //VPN网卡
                        || intf.getDisplayName().contains("VPN")) {
                    continue;
                }
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        String ipaddress = inetAddress.getHostAddress();
                        if (ipaddress.contains("::") || ipaddress.contains("0:0:")
                                || ipaddress.contains("fe80")) {
                            continue;
                        }
                        serverIP = ipaddress;
                    }
                }
                if (!"127.0.0.1".equals(serverIP)) {
                    break;
                }
            }
        } catch (SocketException ex) {
            //ignore
        }
        return serverIP;
    }
}
